package com.lancoo.znbkxx.teacher.ui.activity

import android.content.Context
import android.content.Intent
import android.content.SharedPreferences
import androidx.fragment.app.Fragment
import androidx.viewpager.widget.ViewPager
import androidx.viewpager.widget.ViewPager.OnPageChangeListener
import com.lancoo.cpbase.authentication.base.LoginOperate
import com.lancoo.znbkxx.teacher.R
import com.lancoo.znbkxx.teacher.base.BaseActivity
import com.lancoo.znbkxx.teacher.base.BaseFragment
import com.lancoo.znbkxx.teacher.base.BaseFragmentPagerAdapter
import com.lancoo.znbkxx.teacher.ui.dialog.CommonDialog
import com.lancoo.znbkxx.teacher.ui.fragment.GuideFragment1
import com.lancoo.znbkxx.teacher.ui.fragment.GuideFragment2
import com.lancoo.znbkxx.teacher.ui.fragment.GuideFragment3
import com.lancoo.znbkxx.teacher.ui.fragment.GuideFragment3.CallBackLogin
import java.util.*

class GuideActivity : BaseActivity() {
    private lateinit var operate: LoginOperate
    private var viewPager: ViewPager? = null
    private lateinit var mFragmentList: MutableList<Fragment>
    private lateinit var mBaseFragmentPagerAdapter: BaseFragmentPagerAdapter
    private lateinit var commonDialog: CommonDialog

    override fun isNeedToolBar(): Boolean {
        return false
    }

    override fun setLayout(): Int {
        return R.layout.activity_guide
    }

    private var sp: SharedPreferences? = null

    override fun initView() {
        operate = LoginOperate(this)
        viewPager = findViewById(R.id.vp_guide)
        //        viewPager.setOffscreenPageLimit(2);
        commonDialog = CommonDialog(this, this)
        mFragmentList = ArrayList()
        mFragmentList.add(GuideFragment1())
        mFragmentList.add(GuideFragment2())
        mFragmentList.add(GuideFragment3(CallBackLogin {
            if (application.getSharedPreferences("firstEnter", Context.MODE_PRIVATE)
                            .getBoolean("isFirst", true)) {
                sp = application.getSharedPreferences("firstEnter", Context.MODE_PRIVATE)
                val editor = sp!!.edit()
                editor.putBoolean("isFirst", false)
                editor.apply()
            }
            var intent = Intent(this, SplashActivity::class.java)
            startActivity(intent)
            finish()
        }))

        mBaseFragmentPagerAdapter =
                BaseFragmentPagerAdapter(supportFragmentManager, mFragmentList)
        viewPager!!.setAdapter(mBaseFragmentPagerAdapter)
        viewPager!!.addOnPageChangeListener(object : OnPageChangeListener {
            override fun onPageScrolled(position: Int, positionOffset: Float,
                                        positionOffsetPixels: Int) {
            }

            override fun onPageSelected(position: Int) {}
            override fun onPageScrollStateChanged(state: Int) {}
        })
    }
}